﻿import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text, RichText } from '@tarojs/components'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import './invitepk.scss'

export default class Index extends Component {
  constructor(props) {
    super(props)
    this.state = {
      content1:{},
      content2:[],
      content4:[],
      contents:'',
      contents1:'',
      myTime:null,
      myTime1:null,
      nowDate:'',
      conceal:0,
      annuis:0,
      content3:'',
      grluptype:0
    }
  }
  config = {
    navigationBarTitleText: '加载中'
  }

  componentWillMount () {
    this.getdatas()
  }

  componentWillUnmount () {
    clearTimeout(this.myTime)
    clearTimeout(this.myTime1)
  }
  getdatas(){
    let _this = this
    let date = {
      shareUid:_this.$router.preload.shareUid//'dqouucn2ojikj'//_this.$router.preload.shareUid
    }
    CommonJs.ajaxRequest('/freeGoOrder/getFreeGoOrderByShareUid',date,'GET',function(res){
      // console.log(JSON.stringify(res)+'2222222222')
      // console.log(JSON.stringify(date)+'2222222222')
      // console.log(JSON.stringify(res.content.status)+'333333333333')
      Taro.setNavigationBarTitle({title:"邀请好友PK"})
      if (res.content.status == 3) {
        _this.setState({
          content1: res.content,
          conceal: 2
        })
      } else if (res.content.status == 4 || res.content.status == 5 || res.content.status == 6) {
        _this.setState({
          content1: res.content,
          conceal: 2
        })
      } else {
        _this.setState({
          content1: res.content,
          conceal: 1
        })
      }
      _this.titecl(res.content.theNumberOfRemCoUsers,res.content.status)
      _this.getpklist(res.content.id)
      _this.annucl(res.content.urlType,res.content.status)
      _this.countDown()
    })
  }
  opendetails(){
    let _this = this
    let content1 = _this.state.content1
    let urlType = content1.urlType
    let status = content1.status
    if (status == 2 && urlType == 3) {
      CommonJs.openTo('/pages/my/unpaid')
    } else {
      let aar = []
      let obj = {}
      obj.id = content1.goodsId
      obj.logo = content1.commodityLogo
      obj.commodityName = content1.commodityName
      obj.price = content1.commodityPrice
      obj.amount = content1.amount
      obj.goodsName = content1.goodsName                       
      obj.dealerId = content1.dealerId
      obj.dealerName = ''              
      obj.shopId = content1.shopId
      obj.shopName = content1.shopName
      obj.commodityId = content1.commodityId;  
      obj.needToDistribute = ''
      obj.clusterId = 0
      obj.shopCommodityId = content1.shopCommodityId
      obj.shareUid = _this.$router.preload.shareUid
      obj.businessType = 4
      aar.push(obj)
      _this.$preload({
        aar: aar,
        freetype: 1
      })
      CommonJs.openTo('/pages/pkgo/confirmorder')
    }
  }
  getpklist(freeGoOrderId){
    let _this = this
    let date = {
      freeGoOrderId:freeGoOrderId
    }
    CommonJs.ajaxRequest('/freeGoOrderUser/getFreeGoOrderUserList',date,'GET',function(res){
      // console.log(JSON.stringify(res)+'3333333333')
      let content4 = []
      let peonumber = _this.state.content1.peonumber
      let haspeonumber = res.length
      let nopeonumber = parseInt(peonumber) - parseInt(haspeonumber)
      for (var i = 0; i < nopeonumber; i++) {
        content4.push('wenhaox.png')
      }
      _this.setState({
        content2: res,
        content4: content4
      })
    },'noloading','','hideLoad')
    _this.myTime1 = setTimeout(() => { _this.getpklist(freeGoOrderId) }, 3000)
  }
  getMessage(){
    let _this = this
    let date = {
      type:22
    }
    CommonJs.ajaxRequest('/other/getMessageByType',date,'GET',function(res){
      // console.log(JSON.stringify(res)+'44444444444')
      _this.setState({
        grluptype: 1,
        content3: res.content
      })
    })
  }
  setgrlup(){
    let _this = this
    _this.setState({
      grluptype: 0
    })
  }
  titecl(theNumberOfRemCoUsers,status){
    // console.log(theNumberOfRemCoUsers+'//////////ss22ss//////////'+status)
    let _this = this
    // let contents = ''
    // if (theNumberOfRemCoUsers != 0) {
    //   contents = '还差<span style="color: #C00000;">'+theNumberOfRemCoUsers+'</span>人，人满就可以免单'
    // } else if (status == 4) {
    //   contents = '人数已满，免单成功！'
    // } else if (status == 6) {
    //   contents = '免单时间已到期'
    // } else {
    //   contents = 'PK开始，胜者免单！'
    // }
    let contents = '还差<span style="color: #C00000;">'+theNumberOfRemCoUsers+'</span>人，人满就可以免单'
    _this.setState({
      contents: contents
    })
  }
  annucl(urlType,status){
    // console.log(urlType+'//////////ssss//////////'+status)
    let _this = this
    if (status == 2 && urlType == 0) {
      _this.setState({
        annuis: 2,
        contents1: '我要参与'
      })
    } else if (status == 2 && urlType == -1) {
      _this.setState({
        annuis: 2,
        contents1: '我要参与'
      })
    } else if (status == 2 && urlType == 1) {
      _this.setState({
        annuis: 2,
        contents1: '我要参与'
      })
    } else if (status == 2 && urlType == 2) {
      _this.setState({
        annuis: 1,
        contents1: '点击邀请好友PK'
      })
    } else if (status == 2 && urlType == 3) {
      _this.setState({
        annuis: 2,
        contents1: '已参与，前往支付'
      })
    } 
  }
  countDown(){
    //获取当前时间
    let _this = this
    let date = new Date()
    let nowDate = date.getTime()
    _this.setState({
      nowDate: nowDate
    })
    _this.myTime = setTimeout(() => { _this.countDown() }, 1000)
  }
  countDowns(dates, times){
    //倒计时处理
    let _this = this
    // console.log(JSON.stringify(dates)+'//////1111////'+JSON.stringify(times))
    if (dates >= times) {
      // console.log(JSON.stringify(dates)+'//////2222////'+JSON.stringify(times))
      
      // clearTimeout(_this.myTime)
      // _this.getgoods(1)
    } else {
      return CommonJs.countDowns(dates, times)
    }
  }
  onShareAppMessage(res){
    let title = '火爆全国！是时候展现真正的实力了，是朋友就来PK购'//'考验友谊的时候到了！请帮点一下哟！'
    let path = '/pages/activity/activity?scene='+Taro.getStorageSync('dateinfo').qid+'&shareUid='+this.$router.preload.shareUid
    let imageUrl = this.state.content1.commodityLogo
    return CommonJs.onShareAppMessage(title, path, imageUrl)
  }
  ancocl(status){
    let contents = ''
    if (status == 3) {
      contents = '再接再厉'
    } else if (status == 2) {
      contents = '成功免单'
    } else if (status == 4) {
      contents = 'PK结束'
    } else {
      contents = '为TA打CALL'
    }
    return contents
  }
  docall(freeGoOrderUserId,freeGoOrderId,pkUserId,pkUserName,status){
    if (status != 2 && status != 3 && status != 4) {
      let _this = this
      let date = {
        freeGoOrderUserId:freeGoOrderUserId,
        freeGoOrderId:freeGoOrderId,
        pkUserId:pkUserId,
        pkUserName:pkUserName
      }
      CommonJs.ajaxRequest('/freeGoOrderUserRecord/createFreeGoOrderUserRecord',date,'GET',function(res){
        // console.log(JSON.stringify(res)+'44444444444'+JSON.stringify(date))
        if (res.code != 300) {
          Taro.showToast({
            title:'打call成功',
            icon: 'none',
            duration: 3000
          })
        } else {
          Taro.showToast({
            title:res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },'noloading','noret','hideLoad')
    }
  }
  render () {
    const content1 = this.state.content1
    const content2 = this.state.content2
    const content4 = this.state.content4
    const contents = this.state.contents
    return (
      <View className='page' style={this.state.conceal == 1 ? 'background-color:#c00000;' : 'background-color:#ffffff;'}>
        <View className='qun-tncun' style={this.state.grluptype == 1 ? 'display:block;' : 'display:none;'} onClick={this.setgrlup.bind(this)}>
          <View className='qun-tncuncnt'>
            <View className='qun-tncuis'>
              <RichText nodes={this.state.content3}></RichText>
            </View>
          </View>
        </View>
        <View className='qun-contents1' style={this.state.conceal == 1 ? 'display:block;' : 'display:none;'}>
          <View className='qun-yqmiantimes'>开奖时间: {CommonJs.FormatDate(content1.expiredCollageTime,'')}</View>
          <View className='qun-yqguize' onClick={this.getMessage.bind(this)}>
            <Image className='qun-yqguizeimg' mode='' src='https://minichat.g2ma.com/kedouimg/wenhao.png'></Image>
            <View>规则</View>
          </View>
          <View className='qun-yqinfo'>
            <View className='qun-yqcont'>
              <View className='qun-yqtupian'>
                <Image className='qun-yqtuimg' mode='widthFix' src={content1.commodityLogo ? content1.commodityLogo : 'https://minichat.g2ma.com/kedouimg/studio1.png'}></Image>
                <View className='qun-yqbotm'>
                  <Text className='qun-yqtite'>{content1.commodityName}</Text>
                  <Text className='qun-yqprice'>{content1.commodityPrice}￥</Text>
                </View>
              </View>
              <View className='qun-yqmianinfo'>
                <RichText className='qun-yqmiantime' nodes={contents}></RichText>
                <View className='qun-yqzhuli'>邀请好友助力免单成功率高达99%！</View>
                <View className='qun-yqrenbo'>
                  <View className='qun-yqrens'>
                    {content2.map((item, index) => (
                    <View className='qun-yqrycont'>
                      <View className={item.type == 1 ? 'qun-yqrnyn qun-yqcheck' : 'qun-yqrnyn'}>
                        <Image className='qun-yqtuimg' mode='widthFix' src={item.avatar != null && item.avatar != '' ? item.avatar : 'https://minichat.g2ma.com/kedouimg/ordinary.png'}></Image>
                      </View>
                      <View className='qun-yqlezu' style={item.type == 1 ? 'display:block;' : 'display:none;'}>团长</View>
                    </View>
                    ))}
                    {content4.map((item, index) => (
                    <View className='qun-yqrycont'>
                      <View className='qun-yqrnyn'>
                        <Image className='qun-yqtuimg' mode='widthFix' src={'https://minichat.g2ma.com/kedouimg/'+item}></Image>
                      </View>
                    </View>
                    ))}
                  </View>
                </View>
                <View className='qun-yqpkannu'>
                  <Button openType='share' style={this.state.annuis == 1 ? 'display:block;' : 'display:none;'} className='qun-yqpkannus buttons'>{this.state.contents1}</Button>
                  <View onClick={this.opendetails.bind(this)} style={this.state.annuis == 2 ? 'display:block;' : 'display:none;'} className='qun-yqpkannus'>{this.state.contents1}</View>
                </View>
                <View className='qun-yqdjs'>
                  倒计时{this.countDowns(this.state.nowDate, content1.expiredCollageTime)}后失效
                </View>
                <View className='qun-yqpkxz'>
                  <View className='qun-yqfenge'></View>
                  <Text className='qun-yqxztite'>PK需知</Text>
                  <View className='qun-yqfenge'></View>
                </View>
                <View className='qun-yqxzlebo'>
                  <View className='qun-yqlebos'>
                    <View className='qun-yqli'>
                      <Image className='qun-yqliimg' mode='' src='https://minichat.g2ma.com/kedouimg/pkyi.png'></Image>
                      <View className='qun-yqfahuo'>商品发货</View>
                    </View>
                    <View className='qun-yqli'>
                      <Image className='qun-yqliimg' mode='' src='https://minichat.g2ma.com/kedouimg/tdbuser6.png'></Image>
                      <View className='qun-yqfahuo'></View>
                    </View>
                    <View className='qun-yqli'>
                      <Image className='qun-yqliimg' mode='' src='https://minichat.g2ma.com/kedouimg/pker.png'></Image>
                      <View className='qun-yqfahuo'>邀请好友</View>
                    </View>
                    <View className='qun-yqli'>
                      <Image className='qun-yqliimg' mode='' src='https://minichat.g2ma.com/kedouimg/tdbuser6.png'></Image>
                      <View className='qun-yqfahuo'></View>
                    </View>
                    <View className='qun-yqli'>
                      <Image className='qun-yqliimg' mode='' src='https://minichat.g2ma.com/kedouimg/pksan.png'></Image>
                      <View className='qun-yqfahuo'>人满PK</View>
                    </View>
                    <View className='qun-yqli'>
                      <Image className='qun-yqliimg' mode='' src='https://minichat.g2ma.com/kedouimg/tdbuser6.png'></Image>
                      <View className='qun-yqfahuo'></View>
                    </View>
                    <View className='qun-yqli'>
                      <Image className='qun-yqliimg' mode='' src='https://minichat.g2ma.com/kedouimg/pksi.png'></Image>
                      <View className='qun-yqfahuo'>幸运免单</View>
                    </View>
                  </View>
                </View>
              </View>
            </View>
          </View>
        </View>
        <View className='qun-contents2' style={this.state.conceal == 2 ? 'display:block;' : 'display:none;'}>
          <View className='qun-shopinfo'>
            <View className='qun-splebo'>
              <Image className='qun-spleboimg' mode='widthFix' src={content1.commodityLogo ? content1.commodityLogo : 'https://minichat.g2ma.com/kedouimg/studio1.png'}></Image>
              <View className='qun-splebrig'>
                <View className='qun-sptites qun-spfont'>{content1.commodityName}</View>
                <View className='qun-spxanmn qun-spfont'>{content1.peonumber}人PK，{content1.freenumber}人免费拿</View>
                <View className='qun-spjner qun-spfont'><Text className='qun-sprmb'>￥</Text>{content1.commodityPrice}</View>
                <View className='qun-spdojisi qun-spfont'>倒计时<Text className='qun-sprmb'>{this.countDowns(this.state.nowDate, content1.expiredCollageTime)}</Text>打CALL结束</View>
              </View>
            </View>
            <View className='qun-sppkrs qun-spfont'>{content1.peonumber}人PK</View>
            <View className='qun-sphegs'></View>
            <View className='qun-splbul'>
              {content2.map((item, index) => (
              <View className='qun-splbli'>
                <View className='qun-splblf'>
                  <Image className='qun-splblfimg' mode='' src={item.avatar != null ? item.avatar : 'https://minichat.g2ma.com/kedouimg/ordinary.png'}></Image>
                  <Text className='qun-splfmn qun-spfonts'>{item.nickName}</Text>
                </View>
                <View className='qun-sprg'>
                  <View className='qun-spshuzi qun-spfonts'>{item.votenumber}</View>
                  <View className='qun-spanco qun-spfonting' onClick={this.docall.bind(this,item.id,item.freeGoOrderId,item.accountId,item.accountName,item.status)}>{this.ancocl(item.status)}</View>
                </View>
              </View>
              ))}
            </View>
          </View>
        </View>
      </View>
    )
  }
}

 